<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: {2021/1/7}
 * Time: {14:28}
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\admin\controller;


use app\BaseController;
use app\common\business\AdminUser;
use app\common\lib\Show;
use think\App;
use think\captcha\facade\Captcha;
use think\Exception;
use think\facade\View;

class Login extends BaseController
{
    protected $busObj = null;

    /**
     * Login constructor.
     * @param App $app
     */
    public function __construct(App $app)
    {
        $this->busObj = new AdminUser();
        parent::__construct($app);
    }

    public function index()
    {
        $session = session(config('admin.admin_session_key'));
        if (!empty($session)) {
            return redirect('index/index');
        }
        return View::fetch();
    }

    public function verify()
    {
        return Captcha::create('verify');
    }

    /**
     * @return \think\response\Json
     *
     */
    public function check(): \think\response\Json
    {
        $username = $this->request->param('username', '', 'trim');
        $password = $this->request->param('password', '', 'trim');
        $captcha = $this->request->param('captcha', '', 'trim');

        $data = [
            'username' => $username,
            "password" => $password,
            "captcha" => $captcha
        ];
        //验证
        try {
            $result = $this->busObj->getAdminUserByAdminUser($data);
        } catch (Exception $e) {
            return Show::json_show(config('status.error'), $e->getMessage());
        }
        if ($result) {
            return Show::json_show(config('status.success'), "登录成功");
        } else {
            return Show::json_show(config('status.error'), "登录失败");
        }
    }
}